﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAI : MonoBehaviour
{
    private float speed = 0.05f;
    private Rigidbody2D rig;
    private SpriteRenderer spriteRenderer;
    private Color color;
    /// <summary>
    /// 方向：0上 1下 2左 3右
    /// </summary>
    private int dirId = 0;
    private Vector2 dirVector;
    private float rayDistance = 0.7f;
    private float x;
    private float y;

    private void Awake()
    {
        Physics2D.queriesStartInColliders = false;
        spriteRenderer = GetComponent<SpriteRenderer>();
        color = spriteRenderer.color;
        rig = GetComponent<Rigidbody2D>();
        InitDir(Random.Range(0, 4));
    }

    private void Update()
    {
        if (GameApp.instance.enemyMove)
        {
            //移动
            rig.MovePosition(rig.position + dirVector * speed);
        }
    }

    private void InitDir(int dir)
    {
        dirId = dir;
        switch (dirId)
        {
            case 0:
                dirVector = Vector2.up;
                //控制偏移量
                x = Mathf.Round(rig.position.x) < rig.position.x ? Mathf.Round(rig.position.x) + 0.5f : Mathf.Round(rig.position.x) - 0.5f;
                transform.position = new Vector2(x, transform.position.y);
                break;
            case 1:
                dirVector = Vector2.down;
                x = Mathf.Round(rig.position.x) < rig.position.x ? Mathf.Round(rig.position.x) + 0.5f : Mathf.Round(rig.position.x) - 0.5f;
                transform.position = new Vector2(x, transform.position.y);
                break;
            case 2:
                dirVector = Vector2.left;
                y = Mathf.Round(rig.position.y) < rig.position.y ? Mathf.Round(rig.position.y) + 0.5f : Mathf.Round(rig.position.y) - 0.5f;
                transform.position = new Vector2(transform.position.x, y);
                break;
            case 3:
                dirVector = Vector2.right;
                y = Mathf.Round(rig.position.y) < rig.position.y ? Mathf.Round(rig.position.y) + 0.5f : Mathf.Round(rig.position.y) - 0.5f;
                transform.position = new Vector2(transform.position.x, y);
                break;
            default:
                break;
        }
    }


    private void ChangeDir()
    {
        List<int> dirList = new List<int>();
        if (Physics2D.Raycast(transform.position, Vector2.up, rayDistance).collider == null)
        {
            dirList.Add(0);
        }
        if (Physics2D.Raycast(transform.position, Vector2.down, rayDistance).collider == null)
        {
            dirList.Add(1);
        }
        if (Physics2D.Raycast(transform.position, Vector2.left, rayDistance).collider == null)
        {
            dirList.Add(2);
        }
        if (Physics2D.Raycast(transform.position, Vector2.right, rayDistance).collider == null)
        {
            dirList.Add(3);
        }
        if (dirList.Count > 0)
        {
            int index = Random.Range(0, dirList.Count);
            InitDir(dirList[index]);
        }
    }

    //画辅助线
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, rayDistance, 0));
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, -rayDistance, 0));
        Gizmos.DrawLine(transform.position, transform.position + new Vector3(-rayDistance, 0, 0));
        Gizmos.DrawLine(transform.position, transform.position + new Vector3(rayDistance, 0, 0));
    }

    //碰撞检测
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //碰到层级
        if (collision.gameObject.layer == LayerMask.NameToLayer("Wall") || collision.gameObject.layer == LayerMask.NameToLayer("BrickWall") || collision.gameObject.layer == LayerMask.NameToLayer("Bomb"))
        {
            ChangeDir();
        }
    }

    //检测敌人死亡
    private void OnTriggerEnter2D(Collider2D other)
    {
        //碰到炸弹层级，换方向
        if (other.gameObject.layer == LayerMask.NameToLayer("Bomb"))
        {
            ChangeDir();
        }
        if (other.gameObject.layer == LayerMask.NameToLayer("Explosion"))
        {
            GameApp.instance.deathEnemyCount++;
            GameApp.instance.score++;
            Destroy(gameObject);
        }
    }
}
